package com.ruohi.weixin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.json.JSONObject;

import com.ruohi.util.common.Common;
import com.ruohi.util.common.CommonString;

public class WeiTools {

	/**
	 * 获取access_token
	 * @return 
	 * @throws Exception 
	 */
	public static String getToken(ServletContext application) throws Exception{
		String token = (String) application.getAttribute("token");
		//判断是否到期
		String menuJson = Common.doGet("https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+token);
		JSONObject jo = new JSONObject(menuJson);
		if(token == null || (jo != null && jo.has("errcode"))){//过期
			//token = Common.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxb1446ac86c364796&secret=b69a364b703003a2e71461e2a028bb3b");
			//测试
			token = Common.doGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx95d675ca89c0ce91&secret=6efd01faca45a317919cd2c60e43369f");
			JSONObject jo2 = new JSONObject(token.toString());
			token = (String) jo2.get("access_token");
			application.setAttribute("token", token);
			//System.out.println(token);
		}
		return token;
	}
	
	/**
	 * 生成微信菜单json
	 * @return 
	 * @throws Exception 
	 */
	
	public static String getMenuJson(List<Map> channels,String startNo){
	    StringBuffer tree = new StringBuffer();

	    tree.append("{\"button\":[");
	    String isHave = "";
	    List<Map> oneMaps = new ArrayList(0);
	    String parentNo = "";
	    for (Map map : channels) {
	    	parentNo = map.get("parent_no") == null ? "" : (String)map.get("parent_no");
	    	if (startNo.equals(parentNo)) {
	    		oneMaps.add(map);
	    	}
	    }
	    StringBuffer a = new StringBuffer();
	    int i = 1;
	    for (Map map : oneMaps) {
	    	a.append("{");
	    	String mNoString = CommonString.getFormatPara(map.get("no"));
	    	String mNameString = CommonString.getFormatPara(map.get("name"));
	    	String mTypeString = CommonString.getFormatPara(map.get("type"));
	    	String mUrlString = CommonString.getFormatPara(map.get("url"));
	    	String mKeyString = CommonString.getFormatPara(map.get("key"));
	    	
	    	
	        a.append("\"name\""+":"+"\"" + mNameString + "\",");
	    	//判断有无子菜单
	    	isHave = getChildrenNo(mNoString, channels);
	    	if("".equals(isHave)){
	    		if(mTypeString.equals("")||mTypeString.equals("click")){
	    			if(mKeyString.equals("")){
		        		mKeyString = "key_"+i;
		        	}
	    			a.append("\"type\""+":"+"\"click\",");
	    			a.append("\"key\""+":"+"\"" + mKeyString + "\",");
	    		}else if(mTypeString.equals("view")){
	    			a.append("\"type\""+":"+"\"view\",");
	    			a.append("\"url\""+":"+"\"" + mUrlString + "\",");
	    		}
	    	}
	    	a.append("\"sub_button\""+":"+"[" + isHave + "]");
	    	if(i==oneMaps.size()){
	    		a.append("}");
	    	}else{
	    		a.append("},");
	    	}
	    	i++;
	    }
	    tree.append(a);
	    tree.append("]}");
	    return tree.toString().replace("\r\n", "").replace("\r", "").replace("\n", "");
	  }
	
	/**
	 * 
	 * @return 
	 * @throws Exception 
	 */
	public static String getChildrenNo(String no, List<Map> channels)
	  {
	    StringBuffer child = new StringBuffer();
	    List<Map> children = new ArrayList(0);
	    String parentNo = "";
	    for (Map map : channels) {
	      parentNo = map.get("parent_no") == null ? "" : (String)map.get("parent_no");
	      if (parentNo.equals(no)) {
	        children.add(map);
	      }
	    }
	    if ((children != null) && (children.size() > 0)) {
	      StringBuffer a = new StringBuffer();
	      int j = 1;
	      for (Map map : children) {
	    	  a.append("{");
		    	String mNoString = CommonString.getFormatPara(map.get("no"));
		    	String mNameString = CommonString.getFormatPara(map.get("name"));
		    	String mTypeString = CommonString.getFormatPara(map.get("type"));
		    	String mUrlString = CommonString.getFormatPara(map.get("url"));
		    	String mKeyString = CommonString.getFormatPara(map.get("key"));
		    	
		    	
		        a.append("\"name\""+":"+"\"" + mNameString + "\",");
		        if(mTypeString.equals("")||mTypeString.equals("click")){
		        	if(mKeyString.equals("")){
		        		mKeyString = "key_sub_"+j;
		        	}
	    			a.append("\"type\""+":"+"\"click\",");
	    			a.append("\"key\""+":"+"\"" + mKeyString + "\",");
	    		}else if(mTypeString.equals("view")){
	    			a.append("\"type\""+":"+"\"view\",");
	    			a.append("\"url\""+":"+"\"" + mUrlString + "\",");
	    		}
		    	a.append("\"sub_button\""+":"+"[]");
		    	if(j==children.size()){
		    		a.append("}");
		    	}else{
		    		a.append("},");
		    	}
		    	
		    	j++;
	      }
	      child.append(a);
	    }
	    return child.toString();
	  }
	
	
}
